﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace mvchotleweb.Controllers
{
    [Authorize]
    public class AdminController : Controller
    {
        //
        // GET: /Admin/

        hotel.BLL.tb_user userbll = new hotel.BLL.tb_user();
        hotel.BLL.tb_webinfo webbll = new hotel.BLL.tb_webinfo();

        [Authorize]
        public ActionResult Index()
        {
            hotel.Model.tb_webinfo webmod = new hotel.Model.tb_webinfo();
            webmod = webbll.GetModel(1);
            ViewBag.Message = webmod.web_name;
            ViewData["user"] = (Session["USER"] as hotel.Model.tb_user).username.ToString();
            return View();
        }

        #region  ManageTree 后台树的管理
        //  [HttpPost]
        public ActionResult UserManageTree()
        {
            // hotel.Model.tb_user usermod = Session["USER"] as hotel.Model.tb_user;
            string json = @"[{""id"":1,""text"":""网站信息管理"",""children"":[{""text"":""网站信息管理"",   
                            ""attributes"":{""url"":""/admin/WebinfoMange""}}]},{""text"":""用户管理"",""state"":""closed"", 
                            ""iconCls"":""icon-user_manage"",""children"":[{""text"":""用户管理"",""iconCls"":""icon-user_manage"",
                           ""attributes"":{""url"":""/admin/UserMange""}}]}]";
            string json1 = @"[{""id"": 1,""text"": ""用户管理"",""children"": [{""text"": ""修改密码"",""attributes"": {""url"": ""/admin/ChangePwd""}}]}]";
            //  if (usermod.type!=1)
            //     return Content(json1);
            return Content(json);
        }

        public ActionResult WebManageTree()
        {
            string json = @"[{""id"": 1,""text"": ""前台页面管理"",""children"": [{""text"": ""图片轮播管理"",""attributes"": {""url"": ""/admin/FlashAdManage""}},
                            {""text"": ""网站信息管理"",""attributes"": {""url"": ""/admin/WebinfoMange""}}]}]";
            return Content(json);
        }



        #endregion  ManageTree 后台树的管理


        #region  管理界面调用

        #region  图片轮播管理
        public ActionResult FlashAdManage()
        {
            return View();
        }

        public ActionResult Readpiclist() //读取Flash轮播表
        {
            hotel.BLL.tb_flasad fdll = new hotel.BLL.tb_flasad();
            string res = JsonConvert.SerializeObject(fdll.GetListByPage("", "", 0, 10).Tables[0]);
            return Content(res);
        }
        public ActionResult UpDataImgInfo()
        {
            return View();
        }
       
        public ActionResult AddImg()
        {
            return View();
        }

        public ActionResult DelImg()
        {
            hotel.BLL.tb_flasad fdll = new hotel.BLL.tb_flasad();
            bool res;
            int id = Convert.ToInt32(Request.Form["ID"]);
            res = fdll.Delete(id);
            return Content(res.ToString());
        }
        #endregion  










        [Authorize]
        public ActionResult WebinfoMange()
        {
            return View();
        }

        [Authorize]
        public ActionResult UserMange()
        {
            return View();
        }

        [Authorize]
        public ActionResult ReadUser()
        {
            string res = JsonConvert.SerializeObject(userbll.GetListByPage("", "", 0, 10).Tables[0]);
            return Content(res);
        }
        public ActionResult test()
        {
            return Content(JsonConvert.SerializeObject(Session["USER"]as hotel.Model.tb_user));
        }

        [Authorize]
        public ActionResult AddUser()
        {
            string username = Request.Form["name"];
            string pwd = Request.Form["pwd"];
            string type = Request.Form["type"];
            hotel.Model.tb_user tmpuser = new hotel.Model.tb_user();
            tmpuser.username = username;
            tmpuser.passworld = pwd;
            tmpuser.type =int.Parse(type);
            tmpuser.is_eable = true;
            int res;
            if (userbll.GetModel(username) == null) //判断当前有无该用户
                res = userbll.Add(tmpuser);
            else {
                res = -1;
            }
            return Content(res.ToString());
        }

        [Authorize]
        public ActionResult updatauser()
        {
            bool res;
            string username = Request.Form["name"];
            string pwd = Request.Form["pwd"];
            string type = Request.Form["type"];
            int id = Convert.ToInt32( Request.Form["ID"]);
            hotel.Model.tb_user tmpuser = new hotel.Model.tb_user();
            tmpuser.username = username;
            tmpuser.passworld = pwd;
            tmpuser.type = int.Parse(type);
            tmpuser.is_eable = true;
           
            res = userbll.Updatebyname(tmpuser);
            return Content(res.ToString());
        }
        public ActionResult Deluser()
        {
            bool res;
            int id =Convert.ToInt32( Request.Form["ID"]);
            res = userbll.Delete(id);
            return Content(res.ToString());
        }
        #endregion  管理界面调用
    }
}
